<?php
/**
 * 日志操作
 * 
 * $Id: log.php 121 2010-05-28 07:57:29Z Administrator $
 * $Revision: 121 $
 * $Author: Administrator $
 * $Date: 2010-05-28 15:57:29 +0800 (星期五, 28 五月 2010) $
 * 日志操作代理
 * 
 * @author glzaboy<glzaboy@163.com>
 * @package fastlib
 * @subpackage classes
 * @copyright 2005-2010 (c) glzaboy
 */
if (! defined ( '__FASTLIB__' )) {
	if (__DEBUG__) {
		die ( sprintf ( 'Forbidden 403 at file %s on line %d', __FILE__, __LINE__ ) );
	} else {
		die ( 'Forbidden 403' );
	}
}
/**
 * 日志操作
 * 
 * 日志操作，主要用于系统调试，所有的日志均按站点域名进行保存。
 *
 */
class log {
	/**
	 * 日志时间格式
	 *
	 * @var string
	 */
	private $log_time_format = "Y-m-d H:i:s D";
	/**
	 * 默认日志文件
	 *
	 * @var string
	 */
	private $log_file = "fastlib.log";
	/**
	 * 日志输出格式
	 *
	 * @var unknown_type
	 */
	private $logFormat = "%s at file %s on line %s";
	/**
	 * 日志出错停止
	 *
	 * @var string
	 */
	private $halt = false;
	/**
	 * 网站根目录
	 *
	 * @var string
	 */
	private $siteroot = '';
	/**
	 * 初始化日志类
	 *
	 * @param String $log_time_format 日志格式 eg "%s".date("Y-m-d")
	 * @param String $log_file 日志文件名所有的均相对于__FASTLIB__下的log
	 * @return log
	 */
	public function __construct($log_time_format = null, $log_file = null) {
		global $flibcfg;
		$this->halt = $flibcfg ['log'] ? false : true;
		$log_time_format = $log_time_format ? $log_time_format : $flibcfg ['log_format'];
		$log_file = $log_file ? $log_file : $flibcfg ['log_file'];
		if ($log_time_format) {
			$this->log_time_format = $log_time_format;
		}
		if ($log_file) {
			$this->log_file = $log_file;
		}
		$this->siteroot = getSiteCurrent ( true );
		touchdir ( $this->siteroot . '.temp' );
		touchdir ( $this->siteroot . '.temp/log' );
		touchdir ( $this->siteroot . '.temp/log/' . $flibcfg ['HOST'] . '_' . ($flibcfg ['PORT'] ? $flibcfg ['PORT'] : 80) );
		$this->log_file = $flibcfg ['HOST'] . '_' . ($flibcfg ['PORT'] ? $flibcfg ['PORT'] : 80) . '/' . $this->log_file;
	}
	/**
	 * 增加一条日志
	 *
	 * @param String $message 日志内容
	 * @param String $file 调用文件名
	 * @param Int $line 调用文件行数
	 * @return bool 是否成功写入日志
	 */
	public function putlog($message, $file = 'Invalid', $line = 'Invalid') {
		if ($this->halt) {
			return '';
		}
		return file_put_contents ( $this->siteroot . ".temp/log/" . $this->log_file, date ( $this->log_time_format ) . ' ' . sprintf ( $this->logFormat, $message, $file, $line ) . PHP_EOL, FILE_APPEND );
	}
	/**
	 * 返回本模块信息
	 *
	 * @return string
	 */
	public function printInfo() {
		$version = '$Revision: 121 $';
		$moudle = __CLASS__;
		$return = '';
		$return .= startTable ();
		$return .= printRow ( array (array ('class' => 'h', 'content' => '<center>' . $moudle . '</center>', 'colspan' => '2' ) ) );
		$return .= printRow ( array (array ('class' => 'e', 'content' => 'author' ), array ('class' => 'v', 'content' => 'glzaboy' ) ) );
		$return .= printRow ( array (array ('class' => 'e', 'content' => 'version' ), array ('class' => 'v', 'content' => $version ) ) );
		$return .= printRow ( array (array ('class' => 'e', 'content' => 'domain log suport' ), array ('class' => 'v', 'content' => 'yes' ) ) );
		$return .= printRow ( array (array ('class' => 'e', 'content' => 'Dependence' ), array ('class' => 'v', 'content' => 'System core' ) ) );
		$return .= endTable ();
		return $return;
	}
}